package com.epfresh.api.widget;

import android.app.Activity;
import android.app.Dialog;
import android.content.Context;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.TextView;

import com.epfresh.api.utils.T;
import com.epfresh.supplier.R;
import com.epfresh.supplier.entity.OrderListBean;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

public class OrderSurePrepareDialog extends Dialog implements View.OnClickListener {
    private Context activity;
    private IDialogSure dialogSure;

    OrderListBean dateEntity;
    Date date;

    TextView title, tvFirst, tvSecond, tvThird, tvCancel, tvSure;
    public OrderSurePrepareDialog(Context context, IDialogSure dialogSure, OrderListBean date) {
        this(context, R.style.ActionSheetDialogStyle);
        this.dialogSure = dialogSure;
        this.dateEntity = date;
        initTime();
        builer();
    }

    void initTime(){
        if(dateEntity.getProductDeliveryTime()==null||dateEntity.getProductDeliveryTime().equals("null")){
            date = null;
        }else {
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            try {
                date = sdf.parse(dateEntity.getProductDeliveryTime());
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }

    public OrderSurePrepareDialog(Activity context) {
        this(context, R.style.ActionSheetDialogStyle);
        this.dialogSure = (IDialogSure)context;
        builer();
    }

    public OrderSurePrepareDialog(Context context, int theme) {
        super(context, theme);
        activity = context;
    }

    public OrderSurePrepareDialog builer() {

        View v = LayoutInflater.from(getContext()).inflate(R.layout.dialog_order_prepare, null);

        setContentView(v);
        Window dialogWindow = getWindow();
        dialogWindow.setGravity(Gravity.CENTER_HORIZONTAL);
        WindowManager.LayoutParams lp = dialogWindow.getAttributes();
        dialogWindow.setAttributes(lp);
        tvSure = (TextView)findViewById(R.id.tv_sure);
        tvCancel = (TextView)findViewById(R.id.tv_cancel);
        tvFirst = (TextView)findViewById(R.id.tv_first);
        tvSecond = (TextView)findViewById(R.id.tv_second);
        tvThird = (TextView)findViewById(R.id.tv_third);
        tvSure.setOnClickListener(this);
        tvCancel.setOnClickListener(this);
        tvFirst.setOnClickListener(this);
        tvSecond.setOnClickListener(this);
        tvThird.setOnClickListener(this);
        tvFirst.setText(getNextDay(1));
        tvSecond.setText(getNextDay(2));
        tvThird.setText(getNextDay(3));
        return this;
    }
    public String format(int number){
        if(number<10){
            return "0"+number;
        }
        return ""+number;
    }

    public String getNextDay(int number) {//设置显示日期 三天 第一天，第二天，第三天；
        Calendar calendar = Calendar.getInstance();
        Date dateFlag;
        if(date!=null){
            dateFlag = this.date;
            calendar.setTime(dateFlag);
            calendar.add(Calendar.DAY_OF_MONTH, --number);
        }else {
            dateFlag = new Date();
            calendar.setTime(dateFlag);
            calendar.add(Calendar.DAY_OF_MONTH, number);
        }
        dateFlag = calendar.getTime();
        SimpleDateFormat formatter = new SimpleDateFormat("MM-dd");
        String dateString = formatter.format(dateFlag);
        return dateString;
    }
    public String getAllDay(int number) {//设置传参日期 三天 第一天，第二天，第三天；
        Calendar calendar = Calendar.getInstance();
        if(date!=null){
            Date date = this.date;
            calendar.setTime(date);
        }else {
            Date date = new Date();
            calendar.setTime(date);
        }
        calendar.add(Calendar.DAY_OF_MONTH, number);
        SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
        String dateString = formatter.format(calendar.getTime());
        return dateString;
    }

    @Override
    public void show() {
        super.show();
    }

    String dateString;
    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.tv_cancel:
                dismiss();
                break;
            case R.id.tv_sure:
                if(dateString == null){
                    T.toast("请选择时间");
                    return;
                }
                sure();
                dismiss();
                break;
            case R.id.tv_first:
                tvFirst.setTextColor(0xffF05416);
                tvSecond.setTextColor(0xff333333);
                tvThird.setTextColor(0xff333333);
                dateString = getAllDay(1);
                break;
            case R.id.tv_second:
                tvFirst.setTextColor(0xff333333);
                tvSecond.setTextColor(0xffF05416);
                tvThird.setTextColor(0xff333333);  
                dateString = getAllDay(2);
                break;
            case R.id.tv_third:
                tvFirst.setTextColor(0xff333333);
                tvSecond.setTextColor(0xff333333);
                tvThird.setTextColor(0xffF05416);
                dateString = getAllDay(3);
                break;
        }
    }

    /**
     * 备货完成
     */

    private void sure() {
        dialogSure.orderSure(dateString);
    }

    public interface IDialogSure {
        void orderSure(String dateString);
    }
}
